//
//  DKTabBar.swift
//  Sina
//
//  Created by 赵柏清 on 16/7/29.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit

class DKTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("please implement this method")
    }
    
    // 布局子控件
    override func layoutSubviews() {
        // 调用父类的布局
        super.layoutSubviews()
        
        let width = bounds.size.width / 5
        let high = bounds.size.height
        
        var index = 0
        
        for subview in subviews {
            if subview .isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subview.frame = CGRectMake(CGFloat(index) * width, bounds.origin.y, width, high)
                
                index++
                
                if index == 2 {
                    index++
                }
               
            }
        }
        // 设置中间按钮的frame
        composeBtn.frame = CGRectMake(2.0 * width , 0, width, high)
    }
    
    // MARK: 添加子控件
    private func setupUI(){
        addSubview(composeBtn)
    }
    
    func composeBtnClick(){
        printLog("点击了compose按钮")
    }
    
    // 懒加载
    lazy var composeBtn : UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()

    
    
}
